package com.atguigu.gmall.payment.weixin.service.impl;


import com.atguigu.gmall.common.constant.RabbitConstant;
import com.atguigu.gmall.feign.order.OrderFeignClient;
import com.atguigu.gmall.model.order.entity.OrderInfo;
import com.atguigu.gmall.mq.service.RabbitService;
import com.atguigu.gmall.payment.weixin.config.properties.WxPayConfigProperties;
import com.atguigu.gmall.payment.weixin.pojo.WeiXinVO;
import com.atguigu.gmall.payment.weixin.service.WeiXinService;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import com.wechat.pay.java.service.payments.nativepay.model.QueryOrderByOutTradeNoRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class WeiXinServiceImpl implements WeiXinService {

    @Autowired
    WxPayConfigProperties wxPayConfigProperties;
    @Autowired
    RSAAutoCertificateConfig rsaAutoCertificateConfig;

    @Autowired
    OrderFeignClient orderFeignClient;

    @Autowired
    RabbitService rabbitService;




    @Override
    public WeiXinVO weiXinPay(Long orderId) {

        WeiXinVO weiXinVO = new WeiXinVO();
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId).getData();

        // 初始化服务
        NativePayService service = new NativePayService.Builder().config(rsaAutoCertificateConfig).build();
        PrepayRequest  request = new PrepayRequest ();
        // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
        Amount amount = new Amount();
        amount.setTotal(1);
        amount.setCurrency("CNY");
        request.setAmount(amount);
        request.setAppid(wxPayConfigProperties.getAppid());
        request.setMchid(wxPayConfigProperties.getMchId());
        request.setDescription("手机");
        request.setNotifyUrl(wxPayConfigProperties.getNotifyUrl());

        request.setOutTradeNo(orderInfo.getOutTradeNo());
        // 调用下单方法，得到应答
        PrepayResponse response = service.prepay(request);
        // 使用微信扫描 code_url 对应的二维码，即可体验Native支付
        weiXinVO.setCodeUrl(response.getCodeUrl());
        weiXinVO.setTotalFee(orderInfo.getOriginalTotalAmount());
        weiXinVO.setOrderId(orderId);
        return weiXinVO;
    }

    //   // QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
    //    //    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    //    //    // 调用接口
    //    //    return service.queryOrderByOutTradeNo(request);
    @Override
    public boolean queryPayStatus(long orderId) {
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId).getData();
        // 初始化服务
        NativePayService service = new NativePayService.Builder().config(rsaAutoCertificateConfig).build();

        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        request.setMchid(wxPayConfigProperties.getMchId());
        request.setOutTradeNo(orderInfo.getOutTradeNo());
        Transaction transaction = service.queryOrderByOutTradeNo(request);
        Transaction.TradeStateEnum tradeState = transaction.getTradeState();
        if (tradeState.equals(Transaction.TradeStateEnum.SUCCESS)) {
            //改订单状态
            //发送消息改变订单状态
            Map<String, String> params = new HashMap<>();
            params.put("out_trade_no", transaction.getOutTradeNo());
            params.put("trade_no", transaction.getTransactionId());
            params.put("subject", "尚品汇-"+transaction.getOutTradeNo());
            params.put("trade_status", transaction.getTradeState().name());
            params.put("payment_way", "微信支付");
            rabbitService.sendMsg(RabbitConstant.EXCHANGE_ORDER_EVENT, RabbitConstant.RK_ORDER_PAYED, params);

            return true;
        } else {
            return false;
        }
    }
}
